﻿using DevelopTool.Common;
using DevelopTool.Model.CommonModels;
using Sunny.UI;
using System.Collections.Concurrent;

namespace DevelopTool.Bll.WiFiFixatorBlls
{
    /// <summary>
    /// WiFi监控 日志业务
    /// </summary>
    public static class WiFiFixatorLogBll
    {
        /// <summary>
        /// 队列容量，默认1000个
        /// </summary>
        public static int QueueCapacity = 1000;

        /// <summary>
        /// 先进先出的线程安全队列
        /// </summary>
        private static ConcurrentQueue<LogModel> queue = new ConcurrentQueue<LogModel>();


        /// <summary>
        /// 新增日志
        /// </summary>
        /// <param name="wiFiFixatorLog"></param>
        public static void AddLog(LogModel log)
        {
            queue.Enqueue(log);//将日志对象添加到队列

            #region 检查队列容量
            while (queue.Count > QueueCapacity)
            {
                queue.TryDequeue(out LogModel _);// 移除队列前端的元素
            }
            #endregion 检查队列容量

            if (Setting.System.SendMsgModule.Contains("WiFiFixato"))
                FeiShuMsgHelper.SendCardMsgAsync(log.LogStr, msgType: log.LogType);
        }


        /// <summary>
        /// 从队列中获取第一个日志对象
        /// </summary>
        /// <returns></returns>
        public static LogModel GetLog()
        {
            if (!queue.TryDequeue(out LogModel wiFiFixatorLog)) return null;
            return wiFiFixatorLog;
        }


        /// <summary>
        /// 清空日志队列
        /// </summary>
        /// <param name="wiFiFixatorLog"></param>
        public static void Clear()
        {
            queue.Clear();
        }

    }
}
